package j.framework.core;

import j.framework.base.service.ApplicationService;
import j.util.SpringBeans;

import java.util.List;

/**
 * 应用全局，通过此类可获取到项目配置的一切，也是统一入口
 * User: jason
 * Date: 2014/11/6 13:52
 */
public class FApplication {
    public static final int SQL_LOCK_TIMEOUT = 15000;
    private ApplicationService service;
    private FApplication(){
        this.service = SpringBeans.getBean(ApplicationService.class);
        this.baseResourceUrl = service.getSetting("base_resource_url");
        this.membershipProvider = JUtils.newInstance(MembershipProvider.class, service.getSetting("membership_provider_class"));
    }
    private static FApplication instance = new FApplication();
    public static FApplication getInstance(){
        return instance;
    }

    /**
     * 获取指定key的配置
     * @param key
     * @return
     */
    public static String getSetting(String key){
        return instance.service.getSetting(key);
    }

    private String baseResourceUrl;

    /**
     * 获取指定资源的访问路径
     * @param resource
     * @return
     */
    public static String getResourceUrl(String resource){
        if (resource.toLowerCase().startsWith("http://")) {
            return resource;
        }
        String _path = instance.baseResourceUrl;
        if (!_path.endsWith("/")) {
            _path += "/";
        }
        return _path + resource;
    }

    private MembershipProvider membershipProvider;

    /**
     * 获取统一的用户身份验证
     * @return
     */
    public static MembershipProvider getMembershipProvider(){
        return instance.membershipProvider;
    }

    /**
     * 获取网站名称
     * @return
     */
    public static String getSiteName(){
        return instance.service.getSetting("site.name");
    }

    /**
     * 获取指定模块的实例
     * @param fid
     * @return
     */
    public Function getFunctionById(String fid){
        return service.getFunction(fid);
    }

    public List<DictItem> getDictItems(String dictId){
        return service.getDictItems(dictId);
    }

    public WebRequestConfiguration getWebRequestConfiguration(String reqId) {
        return service.getWebRequestConfiguration(reqId);
    }

    public List<Action> getFunctionActions(String fid) {
        return service.getFunctionActions(fid);
    }
}
